<?php
/**
 */
class TriggersTable extends Doctrine_Table
{
	public function addTriggers($data)
	{
		
		$Triggers = new Triggers();
		$Triggers ->message = $data['message'];
		$Triggers ->added_date = date('Y-m-d H:i:s');
		//echo '<pre>';print_r($Expressions);exit;
		$Triggers ->save();
		return $Triggers->id;
	}
	public function getTriggers($user_id,$currentPage,$resultsPerPage,$serValue='')
	{
		//echo $serValue;exit;
		$query = new Doctrine_Query();
		$query->select('t.*,r.id as relation_id,r.user_id,r.owner_id,r.trigger_id as trigger_id,r.read_status,r.status,m.status,m.owner_id')
		       ->from('Triggers t')
			   ->leftJoin('t.TriggersRelations r ON t.id=r.trigger_id')
			   ->leftJoin('r.MyFavorites m ON m.type_id=r.trigger_id AND type = "triggers" AND m.owner_id='.$user_id)
			   ->leftJoin('r.Users u ON r.owner_id=u.user_id')
			   ->leftJoin('u.UserDetails d ON u.user_id=d.user_id')
			   ->where("r.owner_id='".$user_id."'")
			   ->andWhere("r.status ='Active'")
   			   ->andWhere("r.group_id ='0'")
			   ->andWhere("r.is_owner ='1'")
			   ->orderBy("t.id desc");
			    //echo $serValue;exit;
				if($serValue!='0')
				{
					//echo $serValue;exit;
					$query->andWhere("t.message LIKE '%".$serValue."%' OR CONCAT(u.first_name,u.username) LIKE '%".$serValue."%'");
					
				} else {
					//echo 'oops';exit;
				}
			   //echo $query;exit;
			   require_once('../database/models/Pager.php');
				$pager = new Pager($currentPage,$resultsPerPage);
				//echo $query->getSqlQuery();exit;
				$result = $pager->pagination($query,$serValue);
				//echo '<pre>';print_r($result);exit;
				return $result;
	        
	}
	public function getSharedTriggers($user_id,$currentPage,$resultsPerPage,$serValue='')
	{
		//echo $currentPage."   ,  ",$resultsPerPage;exit;
		$query = new Doctrine_Query();
		$query->select('t.*,r.user_id,r.group_id,r.owner_id,r.read_status,r.status,u.first_name as first_name,g.name,d.photo as photo,m.status,m.owner_id')
		->addSelect('(SELECT COUNT(c.id) FROM Comments c WHERE c.trigger_id=t.id AND c.status="Active") as cnt')
		       ->from('Triggers t')
			   ->leftJoin('t.TriggersRelations r ON t.id=r.trigger_id')
			   ->leftJoin('r.MyFavorites m ON m.type_id=r.trigger_id AND type = "triggers" AND m.owner_id='.$user_id)
			   ->leftJoin('r.Users u ON r.owner_id=u.user_id')
			   ->leftJoin('u.UserDetails d ON u.user_id=d.user_id')
			   ->leftJoin('r.Groups g ON g.group_id=r.group_id')
			   ->where("r.owner_id='".$user_id."' or r.user_id='".$user_id."'")
			   ->andWhere("r.is_owner !='1'")
			   ->andWhere("r.status ='Active'")
			   ->orderBy("r.id desc");
			   //->groupBy('r.trigger_id')
			   //->orderBy("r.trigger_id desc");
			   //echo $query->getSqlQuery();exit;
				if($serValue!='0')
				{
					//echo $serValue;exit;
					$query->andWhere("t.message LIKE '%".$serValue."%' OR CONCAT(u.first_name,u.username) LIKE '%".$serValue."%'");
					
				} else {
					//echo 'oops';exit;
				}
				
			   require_once('../database/models/Pager.php');
				$pager = new Pager($currentPage,$resultsPerPage);
				//echo '<pre>';print_r($pager);exit;
				$result = $pager->pagination($query,$serValue);
				//echo '<pre>';print_r($result);exit;
				return $result;
	        
	}
	////////////////// queries for trigger view   ###############################
	public function getSharedTriggersByTriggerId($user_id,$trigger_id)
	{
		//echo $user_id;exit;
		$query = new Doctrine_Query();
		$query->select('t.*,r.user_id,r.group_id,r.owner_id,u.first_name as first_name,g.name,d.photo as photo,m.status,m.owner_id')
		       ->from('Triggers t')
			   ->leftJoin('t.TriggersRelations r ON t.id=r.trigger_id')
			   ->leftJoin('r.MyFavorites m ON m.type_id=r.trigger_id')
			   ->leftJoin('r.Users u ON r.owner_id=u.user_id')
			   ->leftJoin('u.UserDetails d ON u.user_id=d.user_id')
			   ->leftJoin('r.Groups g ON g.group_id=r.group_id')
			   ->where("r.owner_id='".$user_id."' or r.user_id='".$user_id."'")
			   ->where("r.trigger_id='".$trigger_id."'")
			   ->andWhere("r.is_owner !='1'");
			   $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result[0];
	        
	}
	public function getPersonalTriggersByTriggerId($user_id,$trigger_id)
	{
		//echo $user_id;exit;
		$query = new Doctrine_Query();
		$query->select('t.*,r.group_id,r.user_id,r.owner_id,u.first_name as first_name,d.photo as photo,m.status,m.owner_id')
		       ->from('Triggers t')
			   ->leftJoin('t.TriggersRelations r ON t.id=r.trigger_id')
			   ->leftJoin('r.MyFavorites m ON m.type_id=r.trigger_id')
			   ->leftJoin('r.Users u ON r.owner_id=u.user_id')
			   ->leftJoin('u.UserDetails d ON u.user_id=d.user_id')
			   ->where("r.owner_id='".$user_id."'")
			   ->andWhere("r.trigger_id='".$trigger_id."'")
			   ->andWhere("r.is_owner ='1'");
			   //echo $query;exit;
			   $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result[0];
	        
	}
	public function getTriggerByTriggerId($trigger_id)
	{
		//echo $user_id;exit;
		$query = new Doctrine_Query();
		$query->select('t.message')
		       ->from('Triggers t')
			   ->where('t.id='.$trigger_id);
			   //echo $query;exit;
			   $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result[0];
	        
	}
/// queries for view list in view pageeeee###################33

public function getTriggersCurrent($user_id,$currentPage)
	{
		if($currentPage ==1 || $currentPage=='')
		{
			$limit = 0;
		} else {
			$limit = (($currentPage-1) * 12);
		}
		//echo $serValue;exit;
		$query = new Doctrine_Query();
		$query->select('t.*,r.id as relation_id,r.user_id,r.owner_id,r.trigger_id as trigger_id,r.read_status,r.status,m.status')
		       ->from('Triggers t')
			   ->leftJoin('t.TriggersRelations r ON t.id=r.trigger_id')
			   ->leftJoin('r.MyFavorites m')
			   ->leftJoin('r.Users u ON r.owner_id=u.user_id')
			   ->leftJoin('u.UserDetails d ON u.user_id=d.user_id')
			   ->where("r.owner_id='".$user_id."'")
			   ->andWhere("r.status ='Active'")
   			   ->andWhere("r.group_id ='0'")
			   ->andWhere("r.is_owner ='1'")
			   ->orderBy("t.id desc Limit $limit,12");
			  // ->groupBy("t.id");
			    //echo $serValue;exit;
				//echo $query;exit;
			   $result['result'] = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result;
	        
	}
	public function getSharedTriggersCurrent($user_id,$currentPage,$resultsPerPage)
	{
		if($currentPage ==1 || $currentPage=='')
		{
			$limit = 0;
		} else {
			$limit = (($currentPage-1) * 12);
		}
		//echo $currentPage."   ,  ",$resultsPerPage;exit;
		$query = new Doctrine_Query();
		$query->select('t.*,r.user_id,r.group_id,r.owner_id,r.read_status,u.first_name as first_name,g.name,d.photo as photo,m.status,m.owner_id')
		       ->from('Triggers t')
			   ->leftJoin('t.TriggersRelations r ON t.id=r.trigger_id')
			   ->leftJoin('r.MyFavorites m ON m.type_id=r.trigger_id')
			   ->leftJoin('r.Users u ON r.owner_id=u.user_id')
			   ->leftJoin('u.UserDetails d ON u.user_id=d.user_id')
			   ->leftJoin('r.Groups g ON g.group_id=r.group_id')
			   ->where("r.owner_id='".$user_id."' or r.user_id='".$user_id."'")
			   ->andWhere("r.is_owner !='1'")
			   ->andWhere("r.status ='Active'")
			   ->orderBy("t.id desc");
			   //->groupBy("t.id");
			   //echo $query->getSqlQuery();exit;
				require_once('../database/models/Pager.php');
				$pager = new Pager($currentPage,$resultsPerPage);
				//echo '<pre>';print_r($pager);exit;
				$result = $pager->pagination($query,$serValue);
				//echo '<pre>';print_r($result);exit;
				return $result;
	        
	}
	

}